package com.qf.contract.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.qf.common.base.exception.ServiceException;
import com.qf.common.base.result.ResultCode;
import com.qf.contract.entity.*;
import com.qf.contract.mapper.FContractApprovalMapper;
import com.qf.contract.mapper.FContractMapper;
import com.qf.contract.service.FContractApprovalService;
import com.qf.contract.vo.ContractApprovalViewDetailsVo;
import com.qf.contract.vo.ContractRecordQueryVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 领导审批接口
 */
@Service
public class FContractApprovalServiceImpl implements FContractApprovalService {
    @Resource
    private FContractApprovalMapper fContractApprovalMapper;
    @Resource
    private FContractMapper fContractMapper;

    /**
     * 领导审批接口
     *
     * @param fContractApproval
     * @return
     */
    @Override
    public int leaderApproval(FContractApproval fContractApproval) {
        try {
            return fContractApprovalMapper.updateById(fContractApproval);
        } catch (Exception e) {
            throw new ServiceException(ResultCode.SYS_ERROR);
        }
    }

    /**
     * 查看合同审查流程，需要输入合同id
     *
     * @param id
     * @return
     */
    @Override
    public IPage<ContractRecordQueryVo> contractApprovalView(int id) {
        try {
            MPJLambdaWrapper<FContract> wrapper = new MPJLambdaWrapper<FContract>()
                    .selectAll(FContractApproval.class)//查合同审批表全部字段
                    .selectAll(FSigningUnit.class)//查签订单位的经办人字段
                    .selectAll(FContract.class)//查签订单位的经办人字段
                    .select(FTypeOfContract::getTypeContractName)
                    //表连接
                    .leftJoin(FContractApproval.class, FContractApproval::getContractId, FContract::getContractId)
                    .leftJoin(FSigningUnit.class, FSigningUnit::getUnitId, FContract::getSigningUnit)
                    .leftJoin(FTypeOfContract.class,FTypeOfContract :: getContractId,FContract:: getTypeOfContract)
                    .eq(FContract::getContractId, id);
            Page<ContractRecordQueryVo> contractRecordQueryVoPage = fContractMapper.selectJoinPage(new Page<>(1, 1), ContractRecordQueryVo.class, wrapper);
            return contractRecordQueryVoPage;
        } catch (Exception e) {
            throw new ServiceException(ResultCode.SYS_ERROR);
        }
    }

    /**
     * 合同审批详情
     */
    public IPage<ContractApprovalViewDetailsVo> contractApprovalViewDetails(int id) {
        try {
            MPJLambdaWrapper<FContract> wrapper = new MPJLambdaWrapper<FContract>()
                    .selectAll(FContractApproval.class)//查合同审批表全部字段
                    .selectAll(FSigningUnit.class)//查签订单位的全部字段
                    .selectAll(FContract.class)//查签订单位的经办人字段
                    .selectAll(FContractDetails.class)//查签订单位的全部字段
                    .select(FTypeOfContract::getTypeContractName)
                    //表连接
                    .leftJoin(FContractDetails.class, FContractDetails::getContractId, FContract::getContractDetails)
                    .leftJoin(FTypeOfContract.class,FTypeOfContract :: getContractId,FContract:: getTypeOfContract)
                    .leftJoin(FSigningUnit.class, FSigningUnit::getUnitId, FContract::getSigningUnit)
                    .leftJoin(FContractApproval.class,FContractApproval::getContractId,FContract::getSigningUnit)
                    .eq(FContract::getContractId, id);
            return fContractMapper.selectJoinPage(new Page<>(1, 1), ContractApprovalViewDetailsVo.class, wrapper);
        } catch (Exception e) {
            throw new ServiceException(ResultCode.SYS_ERROR);
        }
    }

}
